package com.nx.edu;

import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author Fxz
 * @version 1.0
 * @date 2021-08-12 8:57
 */
public class NxExecutor {

    private static final String url = "";
    private static final String username = "";
    private static final String password = "";

    @SneakyThrows
    public <T> T query(String sql, Object arg) {
        Order order = new Order();
        // 1、加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2、 获取连接
        Connection conn = DriverManager.getConnection(url, username, password);
        // 3、 创建语句并且执行statement可以理解为是一个执行器
        Statement statement = conn.createStatement();
        sql = String.format(sql,arg);
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            long id = resultSet.getLong("id");
            Long amount = resultSet.getLong("amount");
            long userId = resultSet.getLong("user_id");
            System.out.println("数据库中的信息 " + id + "  " + amount + "  " + userId);
            order.setUserId(userId);
            order.setAmount(amount);
            order.setId(id);
        }
        resultSet.close();
        statement.close();
        conn.close();
        return (T) order;
    }
}
